package com.phone.wxtoken;

import com.phone.servlet.BaseServlet;
import org.json.JSONObject;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by tisen on 2017/8/29.
 */
//@WebServlet(name = "Token", urlPatterns = "/wxtoken", loadOnStartup = 1, initParams =
//        {@WebInitParam(name = "appid", value = "wx5625f36342c1f4c7"), @WebInitParam(name = "appsecret", value = "30c1f8634ee1f21247b565395a1d45a8")})
public class TokenServlet extends BaseServlet {
    private TokenThread tokenThread;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        String appid = config.getInitParameter("appid");
        String appsecret = config.getInitParameter("appsecret");
        tokenThread = new TokenThread();
        tokenThread.setAPPID(appid);
        tokenThread.setAPPSECRET(appsecret);
        //tokenThread.start();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        setParamterRequest(request);
        Result result = null;
        if (tokenThread.getAccessToken() != null) {
            result = new Result("success", null, null, tokenThread.getAccessToken().getAccessToken());
        } else {
            result = new Result("error", tokenThread.getError().getErrcode(), tokenThread.getError().getErrmsg(), null);
        }
        System.out.println(result.toString());
        out(response, new JSONObject(result).toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public class Result {
        private String result;
        private String errcode;
        private String errmsg;
        private String token;

        public Result(String result, String errcode, String errmsg, String token) {
            this.result = result;
            this.errcode = errcode;
            this.errmsg = errmsg;
            this.token = token;
        }

        @Override
        public String toString() {
            return "Result{" +
                    "result='" + result + '\'' +
                    ", errcode='" + errcode + '\'' +
                    ", errmsg='" + errmsg + '\'' +
                    ", wxtoken='" + token + '\'' +
                    '}';
        }

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        public String getErrcode() {
            return errcode;
        }

        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }
    }
}
